package Thread;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws InterruptedException {

            ArrayList<Integer> list = new ArrayList<>();
            Thread t1 = new Thread() {
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        synchronized (list) {
                            list.add(i);
                        }
                    }
                }
            };
            Thread t2 = new Thread() {
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        synchronized (list) {
                            list.add(i);
                        }
                    }
                }
            };
            t1.start();
            t2.start();

            Thread.sleep(1000);//阻塞主线程的目的是等待上述两个线程执行完毕
            System.out.println(list.size());
    }
}